package com.ctcemti.mdm.project.common.excelverifyhandler;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.ctcemti.mdm.project.sys.dao.ResMaterialLibraryMapper;
import com.ctcemti.mdm.project.sys.entity.ResMaterialLibrary;
import tk.mybatis.mapper.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class ExcelVerifyHandlerForResMaterialLibrary implements IExcelVerifyHandler<ResMaterialLibrary> {

    private List<String> listCode;

    private ResMaterialLibraryMapper resMaterialLibraryMapper;

    @Override
    public ExcelVerifyHandlerResult verifyHandler(ResMaterialLibrary resMaterialLibrary) {

        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult();

        if(listCode!=null && listCode.size()>0){
            if(listCode.contains(resMaterialLibrary.getCode())){
                result.setMsg("该物资编码表格中已存在");
                result.setSuccess(false);
                return result;
            }
        }
        ResMaterialLibrary entity = new ResMaterialLibrary();
        entity.setCode(resMaterialLibrary.getCode());
        List<ResMaterialLibrary> list =  resMaterialLibraryMapper.select(entity);
        if(!StringUtil.isEmpty(resMaterialLibrary.getCode()) && list.size()>0){
            result.setMsg("该物资编码数据库中已存在");
            result.setSuccess(false);
            return result;
        }else{
            listCode.add(resMaterialLibrary.getCode());
        }
        result.setSuccess(true);
        return result;
    }

    public ExcelVerifyHandlerForResMaterialLibrary(Object object){
        this.listCode = new ArrayList<String>();
        this.resMaterialLibraryMapper = (ResMaterialLibraryMapper) object;

    }
}
